import {getRequest} from './api'
import {Message} from 'element-ui'

export const isNotNullORBlank = (...args)=> {
  for (var i = 0; i < args.length; i++) {
    var argument = args[i];
    if (argument == null || argument == '' || argument == undefined) {
      Message.warning({message: '数据不能为空!'})
      return false;
    }
  }
  return true;
}
export const initMenu = (router, store)=> {
  if (store.state.routes.length > 0) {
    return;
  }
  getRequest("/config/sysmenu").then(resp=> {
    console.log(resp)
    if (resp && resp.status == 200) {
      var fmtRoutes = formatRoutes(resp.data);
      router.addRoutes(fmtRoutes);
      store.commit('initMenu', fmtRoutes);
      // store.dispatch('connect');
    }
  })
}
export const formatRoutes = (routes)=> {
  let fmRoutes = [];
  routes.forEach(router=> {
    let {
      path,
      component,
      name,
      meta,
      iconCls,
      children
    } = router;
    if (children && children instanceof Array) {
      children = formatRoutes(children);
    }
    let fmRouter = {
      path: path,
      component(resolve){
        //console.log(component)
        if (component.startsWith("Home")) {
          require(['../components/' + component + '.vue'], resolve)
        }  else if (component.startsWith("Sys") || component.startsWith("Advertise")) {
          require(['../components/systemmanage/' + component + '.vue'], resolve)
        } else if (component.startsWith("Course") || component.startsWith("Activity")
          || component.startsWith("MyParticipateList") || component.startsWith("ApproveWorkspace")) {
          require(['../components/coursemanage/' + component + '.vue'], resolve)
        } else if (component.startsWith("ExeciseType") || component.startsWith("ProvinceList") ||
          component.startsWith("Post") ||
          component.startsWith("MajorList") || component.startsWith("BookList") || component.startsWith("SchoolList") ||
          component.startsWith("QuizType") || component.startsWith("SubjectList") || component.startsWith("PeriodList")) {
          require(['../components/metadata/' + component + '.vue'], resolve)
        } else if (component.startsWith("FillList") || component.startsWith("JudgeList") || component.startsWith("SelectList")) {
          require(['../components/TiType/' + component + '.vue'], resolve)
        } else if (component.startsWith("ExeciseList")) {
          require(['../components/execisemanage/' + component + '.vue'], resolve)
        } else if (component.startsWith("Zhibo")) {
          require(['../components/help/' + component + '.vue'], resolve)
        } else if (component.startsWith("Famous")) {
          require(['../components/coursemanage/famous/' + component + '.vue'], resolve)
        } else if (component.startsWith("KenchengDan")) {
          require(['../components/ordermanage/' + 'KenchengDan' + '.vue'], resolve)
        }
		else if (component.startsWith("ShopDingDan")) {
		  require(['../components/ordermanage/' + 'GoodorderList' + '.vue'], resolve)
		}
		 else if (component.startsWith("ExamExecise")) {
          require(['../components/ExamExecise/' + component + '.vue'], resolve)
        } else if (component.startsWith("WordList")) {
          require(['../components/WordList/' + component + '.vue'], resolve)
        } else if (component.startsWith("DailyExecise")) {
          require(['../components/DailyExecise/' + component + '.vue'], resolve)
        } else if (component.startsWith("GroupList")) {
          require(['../components/GroupList/' + component + '.vue'], resolve)
        } else if (component.startsWith("ExamList")) {
          require(['../components/ExamList/' + component + '.vue'], resolve)
        } else if (component.startsWith("CouponList")) {
          require(['../components/CouponList/' + component + '.vue'], resolve)
        } else if (component.startsWith("SetImage")) {
          require(['../components/SetImage/' + component + '.vue'], resolve)
        }


      },
      name: name,
      iconCls: iconCls,
      meta: meta,
      children: children
    };
    fmRoutes.push(fmRouter);
  })
  return fmRoutes;
}
